<style>
    #menu_auth_info .label.label-xlg {
        margin-bottom: 5px;
        margin-right: 5px;
    }

    .help-inline {
        padding-left: 8px
    }
</style>

<div class="modal-header">
    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
        <span aria-hidden="true">&times;</span>
    </button>
    <h4 class="modal-title">添加权限</h4>
</div>
<div class="modal-body">
    <div class="page-content">
        <div class="table-responsive page-content">
            <form class="form-horizontal" role="form" id="validation-form">
                <input type="hidden" name="id" id="id" value="${(permission.id)!''}">
                <div class="form-group">
                    <label class="col-sm-3 control-label no-padding-right" for="permissionName">
                        权限名称
                    </label>
                    <div class="col-sm-9">
                        <input type="text" id="permissionName" name="permissionName"
                               value="${(permission.permissionName)!''}"
                               placeholder="请填写权限名称.."
                               class="col-xs-10 col-sm-5"/>
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-sm-3 control-label no-padding-right" for="permissionCode">
                        权限Code
                    </label>
                    <div class="col-sm-9">
                        <input type="text" id="permissionCode" name="permissionCode"
                               value="${(permission.permissionCode)!''}"
                               placeholder="请填写权限Code.."
                               class="col-xs-10 col-sm-5"/>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>
<div class="modal-footer">
    <button class="btn btn-info" type="button" id="submitPermission">
        <i class="icon-ok bigger-110"></i>
        确认
    </button>
    <button class="btn" type="button" id="cancel">
        <i class="icon-undo bigger-110"></i>
        重置
    </button>
</div>

<script>
    $('#submitPermission').click(function () {
        $('#validation-form').submit();
    });
    var validator = $('#validation-form').validate({
        // 错误展示位置
        errorPlacement: function (error, element) {
            $(element).parent().append(error);
        },
        errorElement: 'label',
        errorClass: 'help-inline',
        focusInvalid: true,
        onfocusout: false,
        rules: {
            permissionName: {
                required: true
            },
            permissionCode: {
                required: true,
                remote: {          //远程ajax验证
                    url: "doCheck.do", //检查是否存在账号，存在则返回true
                    type: "POST",
                    dataType: "json",
                    data: {
                        permissionCode: function () {
                            return $("#permissionCode").val(); //这个是取要验证的用户名
                        }
                    },
                    dataFilter: function (data) {  //判断控制器返回的内容
                        var notice = eval("(" + data + ")");
                        if (notice.result != null && notice.result != $('#id').val()) {
                            return false;
                        } else {
                            return true;
                        }
                    }
                }
            }
        },
        messages: {
            permissionName: {
                required: '请填写权限名'
            },
            permissionCode: {
                required: '请填写权限Code',
                remote: "权限Code已存在！"
            }
        },
        invalidHandler: function (event, validator) { //display error alert on form submit
            $('.alert-danger', $('#validation-form')).show();
        },
        highlight: function (e) {
            $(e).closest('.form-group').removeClass('has-info').addClass('has-error');
        },
        success: function (e) {
            $(e).closest('.form-group').removeClass('has-error').addClass('has-info');
            $(e).remove();
        },
        errorPlacement: function (error, element) {
            error.insertAfter(element);
        },
        submitHandler: function (form) {
            //进行ajax传值
            $.ajax({
                url: "doOperate.do",
                type: "post",
                dataType: "json",
                data: $(form).serializeJson(),
                success: function (msg) {
                    //要执行的代码
                    if (!msg.success) {
                        layer.msg(msg.msg);
                        return;
                    }
                    $(parent.document).find("iframe").each(function () {
                        if ($(this).attr('src').indexOf("permission/list.htm") > -1) {
                            $(this).attr('src', $(this).attr('src'));
                            reinitTreeHight(this);
                        }
                    });
                }
            });
            return false;
        }
    });
    $("#cancel").click(function (e) {
        $('.form-group').removeClass('has-error');
        $('.form-group').removeClass('has-info');
        validator.resetForm();
        $('#validation-form')[0].reset();
    });
</script>